package realQuestion.wande;

public class Main {
    public static void main(String[] args) {
        int[] power_switch = {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1};
        int[] power_switch_off = new int[3];

        if (findOffSwitchByShort(power_switch_off, power_switch)) {
            System.out.print("Closed switches: ");
            for (int i : power_switch_off) {
                System.out.print(i + " ");
            }
        } else {
            System.out.println("No solution found.");
        }
    }

    public static boolean findOffSwitchByShort(int[] power_switch_off, int[] power_switch) {
        return dfs(power_switch_off, power_switch, 0, 0);
    }

    public static boolean dfs(int[] power_switch_off, int[] power_switch, int switchIndex, int count) {
        if (count == 3) {
            return isLightOn(power_switch);
        }

        if (switchIndex >= power_switch.length) {
            return false;
        }

        // Try turning off the current switch
        power_switch[switchIndex] = 0;
        power_switch_off[count] = switchIndex;
        if (dfs(power_switch_off, power_switch, switchIndex + 1, count + 1)) {
            return true;
        }

        // Revert the state and try leaving the current switch on
        power_switch[switchIndex] = 1;
        if (dfs(power_switch_off, power_switch, switchIndex + 1, count)) {
            return true;
        }

        return false;
    }

    public static boolean isLightOn(int[] power_switch) {
        int n = power_switch.length;
        boolean[] isSwitchOn = new boolean[n];
        for (int i = 0; i < n; i++) {
            isSwitchOn[i] = (power_switch[i] == 1);
        }

        for (int i = 0; i < n; i++) {
            if (!isSwitchOn[i]) {
                int left = i;
                int right = i;
                while (left >= 0 && !isSwitchOn[left]) {
                    left--;
                }
                while (right < n && !isSwitchOn[right]) {
                    right++;
                }
                if (left >= 0 && right < n) {
                    return true;
                }
            }
        }
        return false;
    }
}
